$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/lineqcode/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '序列号', name: 'sn', index: 'sn', width: 80 },
			{ label: '二维码编号', name: 'qcode', index: 'qcode', width: 80 },
            // { label: '二维码', name: 'qcodeurl', index: 'qcodeurl', width: 80} ,
			// { label: '二维码', name: 'qcodeurl', index: 'qcodeurl', width: 80 , formatter(value, options, row) {
            //         return '<img  style="width: 50px" src="' + value + '">';
            //     }},
            // { label: '代理商', name: 'agentId', index: 'agent_id', width: 80 },
            /*{ label: '代理商', name: 'agentid', index: 'agentid', width: 65,formatter: function(cellvalue, options, rowdata) {
                    var shopName ;
                    $.ajax({
                        type: "POST",
                        async:false,
                        url: baseURL + "sys/agent/info/"+rowdata.agentid,
                        success: function(r){
                            // shopName = r.agent.shopName;
                            if (null == r.agent) {
                                shopName="";
                            } else {
                                shopName = r.agent.shopName;
                            }
                        }
                    });
                    return shopName;
                }
            },*/
			{ label: '密码', name: 'password', index: 'password', width: 80 },
            { label: '创建时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 100,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return" <a style='cursor:pointer;' class='btn btn-primary' onclick='vm.setSecret("+ rowdata.id +")'>" +
                        "重置密码</a>&nbsp;&nbsp;&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.del("+ rowdata.id +")'>"+
                        "删除</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                }}
        ],//" <a style='cursor:pointer;' onclick='vm.relevancyUser("+ rowdata.id +")'>" +"关联用户</a>&nbsp;&nbsp;&nbsp;&nbsp;"+
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            qcode: null,
            statu: null
        },
        qcx: {
            shopName: null,
            shopPhone: null,
            status: null
        },
		showList: true,
		title: null,
		lineQcode: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
        queryAgent: function () {
            vm.reloadAgent();
        },
		add: function(){
			vm.showList = false;
			// vm.title = "新增";
			// vm.lineQcode = {};
            $('#myModal').modal();
		},
        addAll: function(){
            vm.showList = false;
            vm.title = "新增";
        },
        downLoad : function(event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要下载选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/lineqcode/downCode",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                                vm.reload();
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        // downLoadCode :function (event) {
        //     var id = getSelectedRow();
        //     	if(id == null){
        //     		return ;
        //     	}
        //     $.get(baseURL + "sys/lineqcode/downLoadCode/"+id, function(r){
        //     });
        // },
        downLoadAllCode :function () {
            var lock = false;
            layer.confirm('确定要下载所有的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/lineqcode/downLoadAllCode",
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                                vm.reload();
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
                layer.alert("C:\\Users\\Administrator\\Desktop/线充二维码.xls成功,请到桌面查找！")
                vm.reload();
            }, function(){

            });
        },

        addAll: function () {
            // vm.queryGroup();
            $('#uploadAll').modal();
        },
//上传文件
        saveUpload: function (event) {
            $('#btnSaveUpload').button('loading').delay(1000).queue(function() {
                var crowd_file = $('#files')[0].files[0];
                var formData = new FormData();

                formData.append("files",crowd_file);
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/lineqcode/saveUpload",
                    data: formData,
                    processData : false, // 使数据不做处理
                    contentType : false, // 不要设置Content-Type请求头
                    success: function(r){
                        if(r.code === 0){
                            layer.msg("操作成功", {icon: 1});
                            $('#btnSaveUpload').button('reset');
                            $('#btnSaveUpload').dequeue();
                            vm.reload();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveUpload').button('reset');
                            $('#btnSaveUpload').dequeue();
                        }
                    }
                });
            });
            $('#uploadAll').modal('hide');
        },
        createQcode : function() {
            $('#btnCreateQcode').button('loading').delay(1000).queue(function() {
                            var url = "sys/lineqcode/save";
                            var num = $("#num").val();
                            $.ajax({
                                type: "POST",
                                url: baseURL + url,
                                contentType: "application/json",
                                data: JSON.stringify(num),
                                success: function(r){
                                    if(r.code === 0){
                                         layer.msg("操作成功", {icon: 1});
                                         vm.reload();
                                         $('#btnCreateQcode').button('reset');
                                         $('#btnCreateQcode').dequeue();
                                        $('#myModal').modal("hide");
                                    }else{
                                        layer.alert("请输入1-2000之间的数字");
                                        $('#btnCreateQcode').button('reset');
                                        $('#btnCreateQcode').dequeue();
                                        $('#myModal').modal("show");
                                    }
                                }
                            });
                    	});
        },
		// update: function (event) {
		// 	var id = getSelectedRow();
		// 	if(id == null){
		// 		return ;
		// 	}
		// 	vm.showList = false;
        //     vm.title = "修改";
        //
        //     vm.getInfo(id)
		// },
		// saveOrUpdate: function (event) {
		//     $('#btnCreateQcode').button('loading').delay(1000).queue(function() {
        //         var url = vm.lineQcode.id == null ? "sys/lineqcode/save" : "sys/lineqcode/update";
        //         $.ajax({
        //             type: "POST",
        //             url: baseURL + url,
        //             contentType: "application/json",
        //             data: JSON.stringify(vm.lineQcode),
        //             success: function(r){
        //                 if(r.code === 0){
        //                      layer.msg("操作成功", {icon: 1});
        //                      vm.reload();
        //                      $('#btnSaveOrUpdate').button('reset');
        //                      $('#btnSaveOrUpdate').dequeue();
        //                 }else{
        //                     layer.alert(r.msg);
        //                     $('#btnSaveOrUpdate').button('reset');
        //                     $('#btnSaveOrUpdate').dequeue();
        //                 }
        //             }
        //         });
		// 	});
		// },
        relevancyUser: function(idCode) {
            vm.reloadAgent();
            vm.getInfo(idCode);
            $("#jqGridAdd").jqGrid({
                url: baseURL + 'sys/agent/list',
                datatype: "json",
                colModel: [
                    // { label: 'id', name: 'id', index: 'ID', width: 50, key: true },
                    { label: '代理商名称', name: 'shopName', index: 'SHOP_NAME', width: 120 },
                    { label: '代理商手机', name: 'shopPhone', index: 'SHOP_PHONE', width: 120 },
                    // { label: '代理模式 0 购买设备 1 投放设备', name: 'shopType', index: 'SHOP_TYPE', width: 80 },
                    { label: '代理模式', name: 'shopType', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">购买设备</span>' :
                                '<span class="label label-success">投放设备</span>';
                        }},
                    // { label: '代理商上级', name: 'shopParent', index: 'SHOP_PARENT', width: 80 },
                    { label: '佣金百分比', name: 'shopRatio', index: 'SHOP_RATIO', width: 100 },
                    // { label: '0 可打开所属机柜 1 不可打开', name: 'isopen', index: 'isOPEN', width: 80 },
                    // { label: '可打开所属机柜', name: 'isopen', width: 80, formatter: function(value, options, row){
                    //         return value === 0 ?
                    //             '<span class="label label-danger">是</span>' :
                    //             '<span class="label label-success">否</span>';
                    //     }},
                    // { label: '所属系统', name: 'sysSystem', index: 'SYS_SYSTEM', width: 80 },
                    // { label: '状态 0 正常 1 禁用', name: 'status', index: 'STATUS', width: 80 },
                    { label: '状态', name: 'status', width: 80, formatter: function(value, options, row){
                            return value === 0 ?
                                '<span class="label label-danger">正常</span>' :
                                '<span class="label label-success">禁止</span>';
                        }},
                    { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                            return "<a style='cursor:pointer;' onclick='vm.shopChoose("+ rowdata.id+")'>" +
                                "<span class='btn btn-primary'>选择</span></a>&nbsp;&nbsp;&nbsp;&nbsp;" ;
                        }}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                // multiselect: true,
                pager: "#jqGridPager1",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                }
            });

            $('#myModalAgent').modal();

        },
        shopChoose:function (id) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/agent/info/"+id,
                success: function(r){
                    vm.lineQcode.agentid=r.agent.id;
                }
            });
            vm.updateAgent(vm)
            // vm.reloadAgent();

        },
        updateAgent: function (vm) {
            $('#btnCreateQcode').button('loading').delay(1000).queue(function() {
                // var url = vm.lineQcode.id == null ? "sys/lineqcode/save" : "sys/lineqcode/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/lineqcode/update",
                    contentType: "application/json",
                    data: JSON.stringify(vm.lineQcode),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                            location.reload()
                            $('#myModalAgent').modal("hide");
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                            vm.reload();
                            location.reload()
                            $('#myModalAgent').modal("hide");
                        }
                    }
                });
        	});
            vm.reload();
        },
        setSecret: function (id) {
            var lock = false;
            layer.confirm('确定要重置密码？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/lineqcode/reSetById",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
		del: function (id) {
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/lineqcode/deleteById",
                        contentType: "application/json",
                        data: JSON.stringify(id),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/lineqcode/info/"+id, function(r){
                vm.lineQcode = r.lineQcode;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {'statu': vm.q.statu,
                           'qcode': vm.q.qcode
                },
                page:page
            }).trigger("reloadGrid");
            $('#myModal').modal("hide");
            $('#uploadAll').modal("hide");
		},
        reloadAgent: function (event) {
            var page = $("#jqGridAdd").jqGrid('getGridParam','page');
            $("#jqGridAdd").jqGrid('setGridParam',{
                postData: {'status': vm.qcx.status,
                    'shopName': vm.qcx.shopName,
                    'shopPhone': vm.qcx.shopPhone
                },
                page:page
            }).trigger("reloadGrid");
        },
	}
});